<?php
namespace src\logic;
use \classlib\core as C;
class lv {
	/**
	 * 获取等级图标信息
	 * @param int $lv
	 * @param number $reftype
	 * @param string $lng
	 */
	public static function icon($lv, $reftype=0, $lng='en') {
		static $imap = array();
		($lv < 1) && $lv = 1;
		$reftype = 0;
		if (!isset($imap[$lv][$reftype][$lng])) {
			$where = array('lv'=>$lv, 'lng'=>$lng, 'reftype'=>$reftype);
			$icon  = C\model::load('main.inc.level')->get_value('icon', $where);
			$imap[$lv][$reftype][$lng] = empty($icon)? '':$icon;
		}
		return $imap[$lv][$reftype][$lng];
	}
	
	/**
	 * 获取等级图标信息
	 * @param int $lv
	 * @param number $reftype
	 * @param string $lng
	 */
	public static function exp($lv, $reftype=0, $lng='en') {
		static $imap = array();
		($lv < 1) && $lv = 1;
		$reftype = 0;
		if (!isset($imap[$lv][$reftype][$lng])) {
			$where = array('lv'=>$lv, 'lng'=>$lng, 'reftype'=>$reftype);
			$coin  = C\model::load('main.inc.level')->get_value('coin', $where);
			$imap[$lv][$reftype][$lng] = empty($coin)? 0:$coin;
		}
		return $imap[$lv][$reftype][$lng];
	}
	
	/**
	 * 获取指定的等级资料数据
	 * @param unknown $lv
	 * @param number $reftype 0-主播 1-财富
	 * @return array
	 */
	public static function item($lv, $reftype=0, $lng='en') {
		($lv < 1) && $lv = 1;
		$reftype = 0;
		$where= array('lv'=>$lv, 'lng'=>$lng, 'reftype'=>$reftype);
		$item = C\model::load('main.inc.level')->get_item($where);
		return $item;
	}
	
	/**
	 * 更新附加字段到$data数组中
	 * @param unknown $data
	 * @param unknown $lv
	 * @param number $reftype
	 * @param string $lng
	 */
	public static function merge(&$data, $lv, $reftype=0, $lng='en') {
		$lv = self::item($lv, $reftype, $lng);
		$data['nexp']   = ($lv['coin'] > $data['exp'])? $lv['coin']:$data['exp'];
		$data['lvicon'] = $lv['icon'];
	}
	
	/**
	 * 获取主播等级配置信息 
	 * @param string $lang
	 * @return array
	 */
	public static function config($lng='en') {
		$lvs    = array();
		$fields = 'lv,reftype,icon,coin as exp';
		$where  = array('lng'=>$lng);
		$list   = C\model::load('main.inc.level')->get_list(0, -1, $where, $fields);
		foreach ($list as $item) {
			$lvs[$item['lv']] = array('icon'=>$item['icon'], 'exp'=>$item['exp']);
		}
		return $lvs;
	}
	
	//获取升级的条件判断得到最后的等级
	private static function up($lv, $coin) {
		do {
			$exp = self::exp($lv, 0);
			if ($exp && $coin >= $exp) {
				$lv += 1;
				continue;
			}
			break;
		} while(true);
		return $lv;
	}
	
	/**
	 * 发送直播间的消息通知
	 * @param number $userid
	 * @param number $anchrid
	 */
	 public static function notify($userid, $anchrid, $sid) {
	 	$data = array();
	 	$orm  = C\model::load('main.pl.userinfo');
	 	if ($userid > 0) {//财富等级升级
	 		$user= $orm->set_dev($userid)->get_one($userid);
	 		$lv  = self::up($user['lv'], $user['exp'], 1);
			if ($lv != $user['lv']) {//等级变动更新
			 	$orm->save($userid, array('lv'=>$lv));
			}
			$data = array_merge($data, array('ruin'=>$userid, 'rexp'=>$user['exp'], 'rlv'=>$lv));
	 	}
	 	if ($anchrid > 0) {//主播等级升级
	 		$user= $orm->set_dev($anchrid)->get_one($anchrid);
	 		$lv  = self::up($user['lv'], $user['exp'], 0);
	 		if ($lv != $user['lv']) {//等级变动更新
	 			$orm->save($anchrid, array('lv'=>$lv));
	 		}
	 		$data = array_merge($data, array('auin'=>$anchrid, 'aexp'=>$user['exp'], 'alv'=>$lv));
	 	}
		 if ($sid < 1) {
		 	$sid = limapi::studioid(($userid > 0)? $userid:$anchrid);
		 }
	 	 ($sid > 0) && limapi::notifystudio($sid, json_encode($data), 0, 20017);
	 }
}